package com.you.base.reflect;

import java.lang.reflect.Method;

/**
 * MethodDemo测试 java.lang.reflect.Method.getDeclaringClass()方法的用法
 */
public class MethodDemo {

    public static void main(String[] args) {
        //获取定义类的方法集合
        Method[] methods = SampleClass.class.getMethods();
        //根据方法获取该方法对应的类的类型
        Class declaringClass = methods[0].getDeclaringClass();
        System.out.println(declaringClass.getName());
        //判断类是否接口还是实现类
        System.out.println(declaringClass.isInterface());
        //---------------------------------------------------------
        //获取定义类的方法集合
        methods = SampleInterface.class.getMethods();
        //根据方法获取该方法对应的类的类型
        declaringClass = methods[0].getDeclaringClass();
        System.out.println(declaringClass.getName());
        //判断类是否接口还是实现类
        System.out.println(declaringClass.isInterface());

    }
}

interface SampleInterface {
    public void testInterfaceMethod() throws Exception;
}

class SampleClass {

    private String sampleField;

    public String getSampleField() {
        return sampleField;
    }

    public void setSampleField(String sampleField) {
        this.sampleField = sampleField;
    }

}
